package com.compass.doctor.ui.patient.fragment

import android.os.Bundle
import androidx.fragment.app.viewModels
import com.compass.doctor.callback.UnderwayCallback
import com.compass.doctor.ui.chat.util.CustomUIConfig
import com.compass.doctor.ui.patient.viewmodel.PatientViewModel
import com.compass.framework.constant.CHOOSE_USEFUL
import com.compass.framework.constant.CLINICAL_RECEPTION
import com.compass.framework.constant.KEY_ORDER_ID
import com.compass.framework.constant.KEY_PATIENT_ID
import com.compass.framework.constant.REFRESH_USEFUL
import com.compass.framework.ext.toJson
import com.compass.framework.utils.LiveDataBus
import com.compass.framework.utils.LogUtil
import com.netease.nimlib.sdk.msg.constant.MsgTypeEnum
import com.netease.yunxin.kit.chatkit.ui.`fun`.page.fragment.FunChatP2PFragment
import com.netease.yunxin.kit.chatkit.ui.model.ChatMessageBean

/**
 * Description： 会话界面 (自定义待定)
 * Date:2023/12/23 16:36
 */
class MyChatFragment :FunChatP2PFragment() {

    private  var patientId: String?=null //患者id
    private  var orderId: String?=null //问诊单id
    private val patientViewModel: PatientViewModel by viewModels()
    private var underwayCallback:UnderwayCallback?=null

    override fun initData(bundle: Bundle?) {
        super.initData(bundle)
    }

    override fun initView() {
        super.initView()
        arguments?.let {
            patientId = it.getString(KEY_PATIENT_ID)
            orderId = it.getString(KEY_ORDER_ID)
        }

        LiveDataBus.get().with(CHOOSE_USEFUL,String::class.java).observe(this){
            if(it.isNotEmpty()){
               CustomUIConfig.setReEditText(it)
            }
        }
    }

    fun setUnderwayCallback(underwayCallback:UnderwayCallback){
        this.underwayCallback=underwayCallback
    }

    override fun onReceiveMessage(messageBean: ChatMessageBean?) {
        super.onReceiveMessage(messageBean)
        underwayCallback?.onReceiveMessageCallback()
        LogUtil.d("onReceiveMessage消息内容===${messageBean?.toJson()}")

    }

    override fun sendMessageChange(messageBean: ChatMessageBean?) {
        super.sendMessageChange(messageBean)
        LogUtil.d("消息内容===${messageBean?.toJson()}")
        messageBean?.let {
            patientViewModel.sendMessage(patientId,getMessageType(it),orderId)
        }

    }


    private fun getMessageType(messageBean: ChatMessageBean):String{
        var messageType:String
        when(messageBean.messageData.message.msgType){
            MsgTypeEnum.image->{
                messageType="[图片消息]"
            }
            MsgTypeEnum.audio->{
                messageType="[音频消息]"
            }
            MsgTypeEnum.video->{
                messageType="[视频消息]"
            }
            MsgTypeEnum.location->{
                messageType="[位置消息]"
            }
            MsgTypeEnum.file->{
                messageType="[文件消息]"
            }
            MsgTypeEnum.avchat->{
                messageType="[音视频通话]"
            }
            else -> {
                messageType=messageBean.messageData.message.content
                LogUtil.d("消息内容www===${messageType}")
            }
        }

        return messageType
    }
}